﻿using System;
using System.Windows;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;

namespace PrismInteractionLack
{

	/// <summary>
	/// From http://blog.kutulu.org/2012/02/out-of-control-interactionrequesttrigge.html
	/// it3xl.com: It makes no sense to solve the bug.
	/// </summary>
	public class UnloadableInteractionRequestTrigger : InteractionRequestTrigger
	{
		private readonly Guid _guid = Guid.NewGuid();

		private FrameworkElement _parent;


		protected override void OnAttached()
		{
			base.OnAttached();

			_parent = AssociatedObject as FrameworkElement;
			if (_parent == null)
			{
				return;
			}

			if(_parent.Resources.Contains("Guid"))
			{
				_parent.Resources.Remove("Guid");
			}
			_parent.Resources.Add("Guid", _guid);

			_parent.Unloaded += ParentUnloaded;
		}

		private void ParentUnloaded(object sender, RoutedEventArgs e)
		{
			// It all has no sense!

			Detach();
			SourceObject = null;
		}



	}
}
